package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 482944566@qq.com
 * @version 1.0
 * @date 2024-12-04
 */
@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务，selectGeofence={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
//        需要给每一个围栏VO对象补充车辆数据：绑定车辆总数 可用车辆数
        for (int i=0;i<list.size();i++){
//            取出当前遍历到的围栏ID
            GeofenceVO geofenceVO = list.get(i);
//            封装车辆查询对象，查出当前围栏上绑定的车辆列表
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
//            获取当前围栏绑定的车辆总数
            int totalNum = vehicleVOList.size();
//            定义变量保存可用车辆数
            int availableNum=0;
//            遍历每一辆车，如果车辆状态为1（空闲），可用车辆数++
            for (VehicleVO vehicleVO:vehicleVOList){
                if (vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setTotalNum(totalNum);
//            后续要查看当前围栏上所有车辆的详细信息，所以把查出来的车辆列表也赋值给围栏
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("新增电子围栏数据业务，geofenceParam={}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId()!=null){
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else{
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void removeGeofence(Long id) {
        log.debug("删除电子围栏业务,id={}", id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size()>0 && list!=null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            geofenceMapper.deleteGeofence(id);
        }
    }
}
